const app = getApp();
const util = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentNav: 0,
        scrollTop: 0,
        storeData: '',
        show_fly_img: false,
        cartMoney: 0,
        curpage: 1,
        goodsClass: [],
        classIndex: -2,
        secondClassIndex: 0,
        goodsCurPage: 1,
        each_class_goods: [],
        getRigthReq: false,
        recCurpage: 1,
        rec_goods: [],
        order: '',
        showSorttap: false,
        eachNum: {},
        orderName: '默认排序',
        emptyCart: true,
        showModalStatus: false,
        cartStatus: 'open',
        cartAllselected: true,
        addCartgoodsnum: 0,
        cartGoodslist: {},
        cart_price_sum: 0,
        cartNum: 0,
        seCatindex: 1,
        newCartGoods: {},
        day: '',
        hour: '',
        min: '',
        sec: '',
        isCheck: {},
        goodsImageFly: '',
        floorstatus: false,
        topNum: 0,
        globalImageUrl: app.globalData.imageUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        util.share();
        var self = this;
        this.setData({
            store_id: options.store_id || 86
        });
        wx.getStorage({
            key: 'key',
            success: res => {
                self.setData({
                    key: res.data
                })
            }
        })
        wx.getStorage({
            key: 'latitude',
            success: res => {
                self.setData({
                    lat: res.data
                })
            }
        })
        wx.getStorage({
            key: 'longitude',
            success: res => {
                self.setData({
                    lng: res.data
                })
            }
        })

        this.cartPos = {};
        this.cartPos['x'] = 59;
        this.cartPos['y'] = app.globalData.sh - 9 - 50;
    },
    onShow: function () {
        var self = this;
        wx.getStorage({
            key: 'key',
            success: reskey => {
                self.setData({
                    key: reskey.data
                })
                wx.getStorage({
                    key: 'store_info_' + self.data.store_id + '_key_' + reskey.data,
                    success: (res) => {
                        if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                            self.storeInfoRequest(self.data.store_id, reskey.data);
                        } else {
                            self.storeInfoShowData(res.data, reskey.data);
                        }
                    },
                    fail: (res) => {
                        //获取列表数据
                        self.storeInfoRequest(self.data.store_id, reskey.data);
                    }
                })
            },
            fail: res => {
                wx.getStorage({
                    key: 'store_info_' + self.data.store_id + '_key_',
                    success: (res) => {
                        if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                            self.storeInfoRequest(self.data.store_id, '');
                        } else {
                            self.storeInfoShowData(res.data, '');
                        }
                    },
                    fail: (res) => {
                        //获取列表数据
                        self.storeInfoRequest(self.data.store_id, '');
                    }
                })
            }
        })
    },
    storeInfoRequest: function (store_id, key) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_api&op=store_info_new',
            data: {
                store_id: store_id,
                key: key,
                lat: self.data.lat,
                lng: self.data.lng,
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (res.data.code == 200) {
                    var data = res.data.datas;
                    data.cachTime = Date.parse(new Date());
                    wx.setStorage({
                        key: 'store_info_' + store_id + '_key_' + key,
                        data: data
                    })
                    self.storeInfoShowData(data, key);

                }
            }
        })
    },
    storeInfoShowData: function (data, key) {
        this.setData({
            storeData: data,
            remainTranfee: data.store_info.store_free_price,
            collectedStore: data.store_info.is_favorate
        });
        var promotion = data.promotion;
        if (promotion.xianshi) {
            this.timesPlay(promotion.xianshi.end_time * 1000);
            this.showTime(promotion.xianshi.end_time * 1000);
        }
        if (key) {
            this.getGoodsrec();
            this.getBriefCart(key);
        } else {
            this.getGoodsrec();
        }

    },
    collectStore: function () {
        var self = this;
        if (!this.data.key) {
            wx.navigateTo({
                url: '../login/login'
            })
        } else {
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_favorites_store&op=favorites_add',
                data: {
                    key: self.data.key,
                    store_id: self.data.store_id
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    if (res.data.code == 200) {
                        self.setData({
                            collectedStore: true
                        })
                    }
                }
            })
        }

    },
    cancelCollectStore: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_favorites_store&op=favorites_del',
            data: {
                key: self.data.key,
                store_id: self.data.store_id
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (res.data.code == 200) {
                    self.setData({
                        collectedStore: false
                    })
                }
            }
        })
    },
    timesPlay: function (end_time) {
        var time = util.clock(end_time);
        this.setData({
            'day': time.day,
            'hour': time.hour,
            'min': time.min,
            'sec': time.sec
        })
    },
    showTime: function (end_time) {
        var self = this;
        setInterval(
            function () {
                self.timesPlay(end_time)
            }, 1000);
    },
    getBriefCart: function (key) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=store_cart_count',
            data: {
                key: key,
                store_id: self.data.store_id,
                type: 1
            },
            method: 'GET',
            success: res => {
                if (res.data.datas.cart_goods_num != 0) {
                    var cart_goods_list = res.data.datas.cart_goods_num_list;
                    var cartGoodslist = {};
                    for (let item in cart_goods_list) {
                        cartGoodslist[item] = cart_goods_list[item].goods_num
                    }
                    var cartGoodslistCartid = {};
                    for (let item in cart_goods_list) {
                        cartGoodslistCartid[item] = cart_goods_list[item].cart_id
                    }
                    self.setData({
                        emptyCart: false,
                        cartNum: res.data.datas.cart_goods_num,
                        cartGoodslist: cartGoodslist,
                        cartGoodslistCartid: cartGoodslistCartid,
                        cart_price_sum: (res.data.datas.cart_price_sum * 1.00).toFixed(2),
                    })
                    if (self.data.remainTranfee - self.data.cart_price_sum <= 0) {
                        self.setData({
                            tranFeeleft: '已满' + self.data.remainTranfee + '元免运费'
                        })
                    } else {
                        self.setData({
                            tranFeeleft: '还差' + (self.data.remainTranfee - self.data.cart_price_sum).toFixed(2) + '元免运费'
                        })
                    }
                } else {
                    self.setData({
                        emptyCart: true,
                    })
                }
                if (res.data.datas.cart_price_sum != '0' && res.data.code != '400') {
                    self.setData({
                        shownulltext: true
                    })
                } else {
                    self.setData({
                        shownulltext: false
                    })
                }
            }
        })
    },
    getGoodsrec: function () {
        var self = this;
        var store_id = self.data.store_id;
        var recCurpage = self.data.recCurpage;
        wx.getStorage({
            key: 'get_goodsrec_' + store_id + '_' + recCurpage,
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.getGoodsrecRequest(store_id, recCurpage);
                } else {
                    self.getGoodsrecShowData(res.data);
                }
            },
            fail: () => {
                //获取列表数据
                self.getGoodsrecRequest(store_id, recCurpage);
            }
        })
    },
    getGoodsrecRequest: function (store_id, recCurpage) {
        var self = this;
        if (recCurpage <= 5) {
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=store&op=store_goods',
                data: {
                    store_id: store_id,
                    curpage: recCurpage,
                    page: 10
                },
                method: 'GET',
                success: res => {
                    wx.setStorage({
                        key: 'get_goodsrec_' + store_id + '_' + recCurpage,
                        data: res
                    })
                    self.getGoodsrecShowData(res);
                }
            })
        } else {
            self.setData({
                hasmoreRec: false
            })
        }

    },
    getGoodsrecShowData: function (res) {
        var self = this;
        self.setData({
            rec_goods: self.data.rec_goods.concat(res.data.datas.goods_list),
            hasmoreRec: res.data.hasmore
        })
        if (res.data.hasmore) {
            self.data.recCurpage++
        }
    },
    getGoodsclass: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_api&op=store_goods_class',
            data: {
                store_id: self.data.store_id,
                curpage: self.data.curpage
            },
            method: 'GET',
            success: res => {
                self.setData({
                    goodsClass: self.data.goodsClass.concat(res.data.datas.store_goods_class),
                    hasmore: res.data.datas.hasmore
                })
                if (res.data.datas.hasmore) {
                    self.data.curpage++
                }
            }
        })
    },
    changeCur: function (e) {
        this.setData({
            currentNav: e.target.dataset.current,
            floorstatus: false
        })
    },
    showAllgoods: function (e) {
        this.setData({
            currentNav: e.target.dataset.current
        })
        var self = this;
        var store_id = self.data.store_id;
        wx.getStorage({
            key: 'store_all_promotion_' + store_id,
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.storeAllPromotionRequest(store_id);
                } else {
                    self.storeAllPromotionShowData(res.data);
                }
            },
            fail: () => {
                //获取列表数据
                self.storeAllPromotionRequest(store_id);
            }
        })

        wx.getStorage({
            key: 'store_goods_class_all_left_' + store_id,
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.storeGoodsClassAllLeftRequest(store_id);
                } else {
                    self.storeGoodsClassAllLeftShowData(res.data);
                }
            },
            fail: () => {
                //获取列表数据
                self.storeGoodsClassAllLeftRequest(store_id);
            }
        })


    },
    storeAllPromotionRequest: function (store_id) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_api&op=store_all_promotion',
            data: {
                store_id: store_id
            },
            method: 'GET',
            success: result => {
                var data = result.data.datas;

                data.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'store_all_promotion_' + store_id,
                    data: data
                })
                self.storeAllPromotionShowData(data);
            }
        })
    },
    storeAllPromotionShowData: function (data) {
        var self = this;
        self.setData({
            promotion: data.promotion,
        })
        if (data.promotion.secondkill) {
            self.setData({
                each_class_goods: data.promotion.secondkill.secondkill_goods_list,
                classIndex: -2
            })
        } else if (data.promotion.xianshi) {
            self.setData({
                each_class_goods: data.promotion.xianshi.xianshi_goods_list,
                classIndex: -1
            })
        }
    },
    storeGoodsClassAllLeftRequest: function (store_id) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_api&op=store_goods_class_all_left',
            data: {
                store_id: store_id
            },
            method: 'GET',
            success: res => {
                var data = res.data.datas;

                data.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'store_goods_class_all_left_' + store_id,
                    data: data
                })
                self.storeGoodsClassAllLeftShowData(data);
            }
        })
    },
    storeGoodsClassAllLeftShowData: function (data) {
        var self = this;
        self.setData({
            store_goods_class: data.store_goods_class,
            classGoods_hasmore: data.hasmore,
            defaultId: data.store_goods_class[0].child_list[0].id
        })
        if (self.data.promotion.length == 0) {
            self.setData({
                each_class_goods: data.store_goods_class[0].child_list[0].goods_list,
                classIndex: 0
            })
        }
    },

    changePromotion: function (e) {
        var type = e.currentTarget.dataset.type
        this.setData({
            classIndex: e.currentTarget.dataset.index,
            each_class_goods: this.data.promotion[type][type + '_goods_list']
        })
    },
    changeClass: function (e) {
        this.setData({
            classIndex: e.currentTarget.dataset.index,
            secondClassIndex: 0,
            each_class_goods: [],
            goodsCurPage: 1,
            defaultId: e.currentTarget.dataset.defaultid
        })
        var defaultId = e.currentTarget.dataset.defaultid;
        this.getClassGoods(defaultId, this.data.order);
    },
    changeSecondClass: function (e) {
        if (e.currentTarget.dataset.secatindex) {
            this.setData({
                seCatindex: e.currentTarget.dataset.secatindex,
            })
        }
        this.setData({
            secondClassIndex: e.currentTarget.dataset.index,
            each_class_goods: [],
            goodsCurPage: 1,
            defaultId: e.currentTarget.dataset.defaultid
        })
        var defaultId = e.currentTarget.dataset.defaultid;
        this.getClassGoods(defaultId, this.data.order);
    },
    moreClassGoods: function () {
        if (!this.data.getRigthReq) {
            this.data.goodsCurPage = 2
        }
        var defaultId = this.data.defaultId;
        if (this.data.classGoods_hasmore && this.data.classIndex >= 0) {
            this.getClassGoods(defaultId, this.data.order);
        }
    },
    getSortgoods: function (e) {
        var order = e.currentTarget.dataset.order;
        var orderName = e.currentTarget.dataset.order_name
        this.setData({
            order: order,
            each_class_goods: [],
            goodsCurPage: 1,
            showSorttap: !this.data.showSorttap,
            orderName: orderName
        })
        this.getClassGoods(this.data.defaultId, this.data.order)
    },
    getClassGoods: function (classId, order) {
        var self = this;
        var store_id = self.data.store_id;
        var goodsCurPage = self.data.goodsCurPage;

        wx.getStorage({
            key: 'get_all_goods_by_stc_id_' + store_id + '_' + goodsCurPage + '_' + classId + '_' + order,
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.getClassGoodsRequest(store_id, goodsCurPage, classId, order);

                } else {
                    self.getClassGoodsShowData(res.data);
                }
            },
            fail: () => {
                //获取列表数据
                self.getClassGoodsRequest(store_id, goodsCurPage, classId, order);

            }
        })



    },
    getClassGoodsRequest: function (store_id, goodsCurPage, classId, order) {

        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_api&op=getAllGoodsByStcId',
            data: {
                store_id: store_id,
                stc_id: classId,
                curpage: goodsCurPage,
                order: order
            },
            method: 'GET',
            success: res => {
                var data = res.data;
                data.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'get_all_goods_by_stc_id_' + store_id + '_' + goodsCurPage + '_' + classId + '_' + order,
                    data: data
                })
                self.getClassGoodsShowData(data);

            }
        })
    },
    getClassGoodsShowData: function (data) {
        var self = this;
        self.setData({
            each_class_goods: self.data.each_class_goods.concat(data.datas.goods_list_all),
            classGoods_hasmore: data.datas.hasmore,
            getRigthReq: true
        })
        if (data.datas.hasmore) {
            self.data.goodsCurPage++
        }
    },
    showSorttap: function () {
        this.setData({
            showSorttap: !this.data.showSorttap
        })
    },
    goodDetail: function (e) {
        var goods_id = e.target.dataset.goods_id;
        wx.navigateTo({
            url: '../goods_detail/goods_detail?goods_id=' + goods_id
        })
    },
    cartUp: function (e) {
        var self = this;
        if (self.data.key) {
            var currentStatu = e.currentTarget.dataset.statu;

            if (currentStatu == 'open') {
                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=store_cart_count',
                    method: 'GET',
                    data: {
                        key: self.data.key,
                        store_id: self.data.store_id,
                        type: 2
                    },
                    success: res => {
                        var isCheck = {};
                        var checkedNum = 0;
                        self.setData({
                            cartGoodsAll: res.data.datas[0].goods
                        })
                        for (let i = 0; i < self.data.cartGoodsAll.length; i++) {
                            isCheck[self.data.cartGoodsAll[i].goods_id] = self.data.cartGoodsAll[i].is_check;
                            if (self.data.cartGoodsAll[i].is_check == 0) {
                                self.setData({
                                    cartAllselected: false,
                                })
                            }
                            if (self.data.cartGoodsAll[i].is_check == 1) {
                                checkedNum += (self.data.cartGoodsAll[i].goods_num * 1);
                            }
                            if (self.data.cartGoodsAll[i].is_check != 1) {
                                //未全选
                            }
                            self.setData({
                                isCheck: isCheck,
                                cartselectedNum: checkedNum
                            })
                        }
                    }
                })
                this.setData({
                    cartStatus: 'close'
                })
            } else {
                this.setData({
                    cartStatus: 'open'
                })
            }
            this.bottomUp(currentStatu);
        } else {
            if (app.globalData.authorized) {
                wx.navigateTo({
                    url: "../chooselogin/chooselogin"
                })
            } else {
                wx.navigateTo({
                    url: "../login/login"
                })
            }
        }

    },
    checkboxChange: function (e) {
        var self = this;
        var is_check = 0;
        var cart_id = e.currentTarget.dataset.cart_id;
        var goods_price = e.currentTarget.dataset.goods_price;
        var goods_num = e.currentTarget.dataset.goods_num;
        var goods_id = e.currentTarget.dataset.goods_id;
        var index = e.currentTarget.dataset.index;
        if (e.detail.value == '') {
            is_check = 0;
            self.data.isCheck[goods_id] = '0';
            this.setData({
                cart_price_sum: ((this.data.cart_price_sum * 1.00) - (goods_price * self.data.cartGoodslist[goods_id])).toFixed(2),
                cartselectedNum: this.data.cartselectedNum * 1 - self.data.cartGoodslist[goods_id] * 1,
                cartAllselected: false,
            })
            if (this.data.remainTranfee - this.data.cart_price_sum <= 0) {
                this.setData({
                    tranFeeleft: '已满' + self.data.remainTranfee + '元免运费'
                })
            } else {
                this.setData({
                    tranFeeleft: '还差' + (self.data.remainTranfee - this.data.cart_price_sum).toFixed(2) + '元免运费'
                })
            }
        } else {
            is_check = 1;
            self.data.isCheck[goods_id] = '1';
            for (let k in self.data.isCheck) {
                if (self.data.isCheck[k] == 0) {
                    self.setData({
                        cartAllselected: false
                    })
                    break;
                } else {
                    self.setData({
                        cartAllselected: true
                    })
                }
            }
            this.setData({
                cart_price_sum: ((this.data.cart_price_sum * 1.00) + (goods_price * self.data.cartGoodslist[goods_id])).toFixed(2),
                cartselectedNum: this.data.cartselectedNum * 1 + self.data.cartGoodslist[goods_id] * 1,
            })
            if (this.data.remainTranfee - this.data.cart_price_sum <= 0) {
                this.setData({
                    tranFeeleft: '已满' + self.data.remainTranfee + '元免运费'
                })
            } else {
                this.setData({
                    tranFeeleft: '还差' + (self.data.remainTranfee - this.data.cart_price_sum).toFixed(2) + '元免运费'
                })
            }
        }
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_check',
            data: {
                key: self.data.key,
                cart_id: cart_id,
                is_check: is_check,
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {

            }
        })
        for (let k in this.data.isCheck) {
            if (this.data.isCheck[k] == '1') {
                this.setData({
                    shownulltext: true
                })
                break;
            } else {
                this.setData({
                    shownulltext: false
                })
            }
        }
    },
    cartAllselecte: function (e) {
        var self = this;
        if (e.detail.value == '') {
            //全不选
            this.setData({
                cartAllselected: false
            })
            var checkedGoods = '';
            for (let k in self.data.isCheck) {
                self.data.isCheck[k] = '0'
            }
            for (let i = 0; i < self.data.cartGoodsAll.length; i++) {
                checkedGoods += (self.data.cartGoodsAll[i].cart_id + ',')
            }
            self.setData({
                isCheck: self.data.isCheck,
                tranFeeleft: '还差' + self.data.remainTranfee + '元免运费',
                cartselectedNum: 0
            })
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_check',
                data: {
                    key: self.data.key,
                    cart_id: checkedGoods,
                    is_check: 0,
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    self.setData({
                        cart_price_sum: 0,
                    })
                }
            })
        } else {
            //全选
            this.setData({
                cartAllselected: true
            })
            var uncheckedGoods = '';
            var cartselectedNum = new Number;
            for (let k in self.data.isCheck) {
                self.data.isCheck[k] = '1'
            }
            for (let i = 0; i < self.data.cartGoodsAll.length; i++) {
                uncheckedGoods += (self.data.cartGoodsAll[i].cart_id + ',')
            }
            for (let i in self.data.cartGoodslist) {
                cartselectedNum += (self.data.cartGoodslist[i] * 1)
            }
            self.setData({
                isCheck: self.data.isCheck,
                cartselectedNum: cartselectedNum
            })
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_check',
                data: {
                    key: self.data.key,
                    cart_id: uncheckedGoods,
                    is_check: 1,
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    var allGoodsPrice = 0;
                    for (let i = 0; i < self.data.cartGoodsAll.length; i++) {
                        allGoodsPrice += (self.data.cartGoodsAll[i].goods_price * self.data.cartGoodslist[self.data.cartGoodsAll[i].goods_id])
                    }
                    self.setData({
                        cart_price_sum: allGoodsPrice.toFixed(2),
                    })
                    if (this.data.remainTranfee - this.data.cart_price_sum <= 0) {
                        this.setData({
                            tranFeeleft: '已满' + self.data.remainTranfee + '元免运费'
                        })
                    } else {
                        this.setData({
                            tranFeeleft: '还差' + (self.data.remainTranfee - this.data.cart_price_sum).toFixed(2) + '元免运费'
                        })
                    }
                }
            })
        }
        for (let k in this.data.isCheck) {
            if (this.data.isCheck[k] == 0) {
                this.setData({
                    shownulltext: false
                })
                return;
            } else {
                this.setData({
                    shownulltext: true
                })
            }
        }
    },
    tapCartadd: function (e) {
        var self = this;
        var cart_goods_num = e.currentTarget.dataset.cart_goods_num * 1;
        var cart_id = e.currentTarget.dataset.cart_id;
        var goods_id = e.currentTarget.dataset.goods_id;
        var price = e.currentTarget.dataset.price;
        var index = e.currentTarget.dataset.index;
        var ifCheck = this.data.isCheck[goods_id];
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_edit_quantity',
            data: {
                key: self.data.key,
                cart_id: cart_id,
                quantity: self.data.cartGoodslist[goods_id] * 1 + 1,
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (res.data.code == 200) {
                    self.data.cartGoodslist[goods_id] = self.data.cartGoodslist[goods_id] * 1 + 1;
                    if (ifCheck == '1') {
                        self.setData({
                            cartGoodslist: self.data.cartGoodslist,
                            cartNum: self.data.cartNum * 1 + 1,
                            cartselectedNum: self.data.cartselectedNum * 1 + 1,
                            cart_price_sum: (self.data.cart_price_sum * 1 + price * 1).toFixed(2)
                        })
                    } else if (ifCheck == '0') {
                        self.setData({
                            cartGoodslist: self.data.cartGoodslist,
                            cartNum: self.data.cartNum * 1 + 1,
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            }
        })
    },
    tapCartcut: function (e) {
        var self = this;
        var cart_goods_num = e.currentTarget.dataset.cart_goods_num * 1;
        var cart_id = e.currentTarget.dataset.cart_id;
        var goods_id = e.currentTarget.dataset.goods_id;
        var price = e.currentTarget.dataset.price;
        var index = e.currentTarget.dataset.index;
        var ifCheck = this.data.isCheck[goods_id];
        if (this.data.cartGoodslist[goods_id] == 1 || this.data.cartGoodslist[goods_id] == 0) {
            wx.showModal({
                title: '温馨提示',
                content: '确定删除此商品吗？',
                success: res => {
                    if (res.confirm) {
                        //调用删除接口
                        wx.request({
                            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_del',
                            data: {
                                key: self.data.key,
                                cart_id: cart_id
                            },
                            method: 'POST',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            dataType: 'json',
                            success: res => {
                                if (res.data.code == 200) {
                                    for (let i = 0; i < self.data.cartGoodsAll.length; i++) {
                                        if (self.data.cartGoodsAll[i].goods_id == goods_id) {
                                            self.data.cartGoodsAll.splice(i, 1)
                                        }
                                    }
                                    if (self.data.cartGoodsAll.length == 0) {
                                        self.setData({
                                            showModalStatus: false,
                                            emptyCart: true
                                        })
                                    }
                                    self.data.cartGoodslist[goods_id] = 0;
                                    if (self.data.isCheck[goods_id] != 0) {
                                        self.setData({
                                            cartGoodsAll: self.data.cartGoodsAll,
                                            cartGoodslist: self.data.cartGoodslist,
                                            cart_price_sum: (self.data.cart_price_sum * 1 - price * 1).toFixed(2),
                                            cartNum: self.data.cartGoodslist[goods_id] != 0 ? self.data.cartNum * 1 - 1 : self.data.cartNum * 1,
                                            cartselectedNum: self.data.cartGoodslist[goods_id] != 0 ? self.data.cartselectedNum * 1 - 1 : self.data.cartselectedNum * 1,
                                        })
                                    } else {
                                        self.setData({
                                            cartGoodsAll: self.data.cartGoodsAll,
                                            cartGoodslist: self.data.cartGoodslist,
                                            cartNum: self.data.cartGoodslist[goods_id] != 0 ? self.data.cartNum * 1 - 1 : self.data.cartNum * 1
                                        })
                                    }
                                    delete self.data.isCheck[goods_id];
                                    self.checkAllselected();
                                }
                            }
                        })
                    }
                }
            })
        } else {
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_edit_quantity',
                data: {
                    key: self.data.key,
                    cart_id: cart_id,
                    quantity: self.data.cartGoodslist[goods_id] * 1 - 1
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    if (res.data.code == 200) {
                        self.data.cartGoodslist[goods_id] = self.data.cartGoodslist[goods_id] * 1 - 1;
                        if (ifCheck == '1') {
                            self.setData({
                                cartGoodslist: self.data.cartGoodslist,
                                cartNum: self.data.cartNum * 1 - 1,
                                cartselectedNum: self.data.cartselectedNum * 1 - 1,
                                cart_price_sum: (self.data.cart_price_sum * 1 - price * 1).toFixed(2)
                            })
                        } else if (ifCheck == '0') {
                            self.setData({
                                cartGoodslist: self.data.cartGoodslist,
                                cartNum: self.data.cartNum * 1 - 1,
                            })
                        }
                    } else {
                        wx.showToast({
                            title: res.data.datas.error,
                            icon: 'none'
                        })
                    }
                }
            })
        }
    },
    checkAllselected: function () {
        var isCheck = this.data.isCheck;
        for (let k in isCheck) {
            if (isCheck[k] == 0) {
                this.setData({
                    cartAllselected: false
                })
                break;
            } else {
                this.setData({
                    cartAllselected: true
                })
            }
        }
    },
    indexAddcart: function (e) {
        var self = this;
        wx.getStorage({
            key: 'key',
            success: res => {
                self.setData({
                    key: res.data
                })
                var goods_id = e.currentTarget.dataset.goods_id;
                var goods_img = e.currentTarget.dataset.goods_img;
                this.setData({
                    goodsImageFly: goods_img
                })
                var price = e.currentTarget.dataset.price * 1.00.toFixed(2);
                this.data.eachNum[goods_id] = this.data.cartNum + 1

                if (!this.data.cartGoodslist[goods_id]) {
                    this.data.cartGoodslist[goods_id] = 0;
                }

                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_add_edit',
                    data: {
                        key: self.data.key,
                        goods_id: goods_id,
                        quantity: self.data.cartGoodslist[goods_id] * 1 + 1,
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    success: res => {
                        if (res.data.code == 200) {
                            self.addCart(e);
                            self.data.isCheck[goods_id] = 1;
                            if (self.data.cartGoodslist[goods_id] == 0) {
                                self.data.newCartGoods[goods_id] = res.data.datas;
                                self.setData({
                                    newCartGoods: self.data.newCartGoods
                                })
                            }
                            self.data.cartGoodslist[goods_id] = this.data.cartGoodslist[goods_id] * 1 + 1;
                            self.setData({
                                isCheck: self.data.isCheck,
                                cartGoodslist: self.data.cartGoodslist,
                                cart_price_sum: (self.data.cart_price_sum * 1 + price * 1).toFixed(2),
                                emptyCart: false,
                                cartNum: self.data.cartNum * 1 + 1,
                                cartselectedNum: self.data.cartselectedNum * 1 + 1,
                            })
                            wx.getStorage({
                                key: 'cart_count',
                                success: (res) => {
                                    wx.setStorage({
                                        key: 'cart_count',
                                        data: res.data + 1
                                    })
                                },
                            })
                            if (self.data.remainTranfee - self.data.cart_price_sum <= 0) {
                                self.setData({
                                    tranFeeleft: '已满' + self.data.remainTranfee + '元免运费'
                                })
                            } else {
                                self.setData({
                                    tranFeeleft: '还差' + (self.data.remainTranfee - self.data.cart_price_sum).toFixed(2) + '元免运费'
                                })
                            }
                        } else {
                            wx.showToast({
                                title: res.data.datas.error,
                                icon: 'none'
                            })
                        }

                    }
                })
            },
            fail: () => {
                if (app.globalData.authorized) {
                    wx.navigateTo({
                        url: "../chooselogin/chooselogin"
                    })
                } else {
                    wx.navigateTo({
                        url: "../login/login"
                    })
                }
            }
        })

    },
    indexCutcart: function (e) {
        var self = this;
        var price = e.currentTarget.dataset.price;
        var goods_id = e.currentTarget.dataset.goods_id;
        if (e.currentTarget.dataset.cart_id) {
            var cart_id = e.currentTarget.dataset.cart_id;
        } else {
            var cart_id = this.data.newCartGoods[goods_id];
        }
        if (this.data.cartGoodslist[goods_id] != 1 && this.data.cartGoodslist[goods_id] != 0) {
            this.data.cartGoodslist[goods_id] = this.data.cartGoodslist[goods_id] * 1 - 1;
            if (this.data.isCheck[goods_id] != 0) {
                this.setData({
                    cart_price_sum: (this.data.cart_price_sum * 1 - price * 1).toFixed(2),
                    emptyCart: false,
                    cartselectedNum: self.data.cartselectedNum * 1 - 1,
                })
            }
            this.setData({
                cartGoodslist: this.data.cartGoodslist,
                cartNum: this.data.cartNum * 1 - 1
            })

            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_add_edit',
                data: {
                    key: self.data.key,
                    goods_id: goods_id,
                    quantity: self.data.cartGoodslist[goods_id] * 1,
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: res => {
                    wx.getStorage({
                        key: 'cart_count',
                        success: (res) => {
                            wx.setStorage({
                                key: 'cart_count',
                                data: res.data - 1
                            })
                        },
                    })
                }
            })
        } else {
            wx.showModal({
                title: '温馨提示',
                content: '确定要删除该商品吗？',
                success: res => {
                    if (res.confirm) {
                        //调用删除接口
                        wx.request({
                            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_del',
                            data: {
                                key: self.data.key,
                                cart_id: cart_id
                            },
                            method: 'POST',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            dataType: 'json',
                            success: res => {
                                if (res.data.code == 200) {
                                    if (self.data.cartGoodslist[goods_id] != 0) {
                                        wx.getStorage({
                                            key: 'cart_count',
                                            success: (res) => {
                                                wx.setStorage({
                                                    key: 'cart_count',
                                                    data: res.data - 1
                                                })
                                            },
                                        })
                                    }

                                    console.log(self.data.cartGoodslist[goods_id])
                                    if (this.data.isCheck[goods_id] != 0) {
                                        self.setData({
                                            cart_price_sum: (self.data.cart_price_sum * 1 - price * 1).toFixed(2),
                                            cartselectedNum: self.data.cartGoodslist[goods_id] != 0 ? self.data.cartselectedNum * 1 - 1 : self.data.cartselectedNum * 1,
                                        })
                                    }
                                    self.setData({
                                        cartNum: self.data.cartGoodslist[goods_id] != 0 ? self.data.cartNum * 1 - 1 : self.data.cartNum * 1,
                                    })
                                    delete self.data.cartGoodslist[goods_id];
                                    self.setData({
                                        cartGoodslist: self.data.cartGoodslist,
                                    })
                                }
                                if (JSON.stringify(self.data.cartGoodslist) == '{}') {
                                    self.setData({
                                        emptyCart: true
                                    })
                                }
                            }
                        })
                    }
                }
            })
        }

    },
    addspeCart: function (e) {
        this.addCart(e);
        var self = this;
        var goods_id = e.currentTarget.dataset.goods_id;
        var price = e.currentTarget.dataset.price;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=wechat_cart_add',
            data: {
                key: self.data.key,
                goods_id: goods_id,
                quantity: 1
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
                if (res.data.code == 200) {
                    wx.getStorage({
                        key: 'cart_count',
                        success: (res) => {
                            wx.setStorage({
                                key: 'cart_count',
                                data: res.data + 1
                            })
                        },
                    })
                    self.setData({
                        cart_price_sum: (self.data.cart_price_sum * 1 + price * 1).toFixed(2),
                        cartNum: self.data.cartNum * 1 + 1
                    })
                }
            }
        })
    },
    addCart: function (e) {
        if (this.data.show_fly_img)
            return;
        this.finger = {};
        var topPoint = {};
        this.finger['x'] = e.touches["0"].clientX;
        this.finger['y'] = e.touches["0"].clientY;
        if (this.finger['x'] - this.cartPos['x'] < 0) {
            topPoint['x'] = (this.cartPos['x'] - this.finger['x']) / 2 + this.finger['x'];
        } else {
            topPoint['x'] = Math.abs(this.cartPos['x'] - this.finger['x']) / 2 + this.cartPos['x'];
        }
        if (this.finger['y'] < this.cartPos['y']) {
            topPoint['y'] = this.finger['y'] - 80;
        } else {
            topPoint['y'] = this.cartPos['y'] - 80;
        }
        this.linePos = app.bezier([this.finger, topPoint, this.cartPos], 20);
        this.startAnimation();
    },
    bottomUp: function (statu) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'linear',
            delay: 0
        })
        this.animation = animation;
        animation.translateY(400).step();
        this.setData({
            animationDataCart: animation.export()
        })
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationDataCart: animation
            })
        }.bind(this), 200)
        if (statu == 'open') {
            this.setData({
                showModalStatus: true
            })
        }
        if (statu == 'close') {
            this.setData({
                showModalStatus: false
            })
        }
    },
    startAnimation: function () {
        var index = 0,
            self = this,
            bezier_points = this.linePos['bezier_points'];
        this.setData({
            show_fly_img: true,
            fly_x: self.finger['x'],
            fly_y: self.finger['y']
        })
        this.timer = setInterval(() => {
            self.setData({
                fly_x: bezier_points[index]['x'],
                fly_y: bezier_points[index]['y']
            })
            index++;
            if (index >= 20) {
                clearInterval(self.timer);
                self.setData({
                    show_fly_img: false
                })
            }
        }, 16);
    },
    storeCartcalc: function () {
        var self = this;
        if (self.data.key) {
            if (this.data.cartselectedNum != '0') {
                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=store_cart_count',
                    method: 'GET',
                    data: {
                        key: self.data.key,
                        store_id: self.data.store_id,
                        type: 2
                    },
                    success: res => {
                        var cart_id = [];
                        var goods = res.data.datas[0].goods;
                        for (let i = 0; i < goods.length; i++) {
                            if (goods[i].is_check != '0') {
                                cart_id[i] = goods[i].cart_id + '|' + goods[i].goods_num
                            }
                        }
                        wx.navigateTo({
                            url: '../confirm_order/confirm_order?ifcart=1&cart_id=' + cart_id
                        })
                    }
                })
            }
        } else {
            if (app.globalData.authorized) {
                wx.navigateTo({
                    url: "../chooselogin/chooselogin"
                })
            } else {
                wx.navigateTo({
                    url: "../login/login"
                })
            }
        }


    },
    showSecatindex: function (e) {
        this.setData({
            seCatindex: e.currentTarget.dataset.secatindex
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.hasmoreRec) {
            this.getGoodsrec()
        }
    },
    /**
     * 页面滚动事件的处理函数
     */
    onPageScroll: function (e) {
        this.setData({
            scrollTop: e.scrollTop
        })
    },
    viewScroll: function (e) {
        if (e.detail.scrollTop > 100) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    goTop: function (e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    //回到顶部
    toTop: function (e) { // 一键回到顶部
        this.setData({
            topNum: 0
        });
    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.storeData.store_info.store_name,
            path: share_path
        }
    }
})